package com.wave.gate.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class SocketClientUtil {

    private final static Logger logger = LoggerFactory.getLogger(SocketClientUtil.class);
    private static Socket socket = null;

    /**
     * 创建socket 发送数据
     *
     * @param message
     */
    public static String sendData(Socket netController, String message) {
        String ret = "";
        try {
            //输出流
            OutputStream os = netController.getOutputStream();
            BufferedOutputStream bw = new BufferedOutputStream(os);
            byte[] byteInput = hexStr2bytes(message);
            //向服务端写入数据
            bw.write(byteInput);
            bw.flush();

            //输入流
            InputStream is = netController.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] bytes = new byte[10]; // 一次读取一个byte

            bis.read(bytes);
            ret += bytesToHexString(bytes) + " ";  //调用字节转化16进制字符串方法
        } catch (Exception e) {
            logger.error("sendData error:[{}]", e.getMessage(), e);
        }
        return ret;
    }

    /**
     * 创建socket 发送数据
     *
     * @param message
     */
    public static String sendTailgatingData(Socket netController, String message) {
        String ret = "";
        try {
            //输出流
            OutputStream os = netController.getOutputStream();
            BufferedOutputStream bw = new BufferedOutputStream(os);
            byte[] byteInput = hexStr2bytes(message);
            //向服务端写入数据
            bw.write(byteInput);
            bw.flush();

            //输入流
            InputStream is = netController.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] bytes = new byte[13]; // 一次读取一个byte

            bis.read(bytes);
            ret += bytesToHexString(bytes) + " ";  //调用字节转化16进制字符串方法
        } catch (Exception e) {
            logger.error("sendData error:[{}]", e.getMessage(), e);
        }
        return ret;
    }

    private static byte[] hexStr2bytes(String hexStr) {
        //长度为单数
        if (hexStr.length() % 2 != 0) {
            hexStr = "0" + hexStr;//前面补0
        }
        char[] chars = hexStr.toCharArray();
        int len = chars.length / 2;
        byte[] bytes = new byte[len];
        for (int i = 0; i < len; i++) {
            int x = i * 2;
            bytes[i] = (byte) Integer.parseInt(String.valueOf(new char[]{chars[x], chars[x + 1]}), 16);
        }
        return bytes;
    }

    private static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            stringBuilder.append("0x");

            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
            if (i != src.length - 1) {
                stringBuilder.append(",");
            }
        }
        return stringBuilder.toString();
    }
}
